//
// Created by pzw on 24-3-12.
//

#include "Stack.h"

template<typename E>
Stack<E>::Stack(std::stack<E> *stk):stack(stk) {
}

template<typename E>
Stack<E>::~Stack() {
    delete stack;
}

template<typename E>
bool Stack<E>::empty() {
    return (*stack).empty();
}

template<typename E>
E Stack<E>::peek() {
    return (*stack).top();
}

template<typename E>
E Stack<E>::pop() {
    E topElement = (*stack).top();
    (*stack).pop();
    return topElement;
}

template<typename E>
E Stack<E>::push(E item) {
    (*stack).push(item);
    return item;
}

template<typename E>
int Stack<E>::search(E item) {
    int position = 1;
    std::stack<E> tempStack = (*stack);

    while (!tempStack.empty()) {
        if (tempStack.top() == item) {
            return position;
        }
        tempStack.pop();
        position++;
    }

    return -1; // Object not found
}

int Stack_main() {
    Stack<int> *stack = new Stack<int>(new std::stack<int>());
    for (int i = 0; i < 8; ++i) {
        stack->push(i);
    }
    int pop = stack->pop();
    std::cout << "Stack_main: pop = " << pop << std::endl;

    int search = stack->search(8);
    std::cout << "Stack_main: search = " << search << std::endl;

    int peek = stack->peek();
    std::cout << "Stack_main: peek = " << peek << std::endl;

    int empty = stack->empty();
    std::cout << "Stack_main: empty = " << empty << std::endl;

    delete stack;
    return 0;
}